package com.lzh.educenter.controller;


import com.lzh.commonutils.JwtUtils;
import com.lzh.commonutils.R;
import com.lzh.commonutils.vo.UcenterMemberOrder;
import com.lzh.commonutils.vo.UcenterMemberVo;
import com.lzh.educenter.entity.UcenterMember;
import com.lzh.educenter.entity.vo.LoginVo;
import com.lzh.educenter.entity.vo.RegisterVo;
import com.lzh.educenter.service.UcenterMemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author lzh
 * @since 2021-03-07
 */
@RestController
@RequestMapping("/educenter/member")
@CrossOrigin
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService  memberService;

    //登录的方法
    @PostMapping("login")
    public R loginUser(@RequestBody UcenterMember member) {
        //调用service方法实现登录,因为是单点登录，登录之后要返回一个token字符串(使用jwt生成)，通过地址栏返回回来
        String token = memberService.login(member);
        return R.ok().data("token",token);
    }

    //注册的方法
    @PostMapping("register")
    public R register(@RequestBody RegisterVo registerVo){
        memberService.register(registerVo);
        return R.ok();
    }

    //根据token获取用户信息
    @GetMapping("getMemberInfo")
    public R getMemberInfo(HttpServletRequest request) {
        //调用jwt工具类的方法。根据request对象获取头信息，返回用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        //查询数据库根据用户id获取用户信息
        UcenterMember member = memberService.getById(memberId);
        return R.ok().data("userInfo",member);
    }


    /**
     *  获取会员信息：
     *  方法名：getUserInfoByToken
     *  参数：HttpServletRequest request
     *  返回值：R
     */
    @GetMapping("getMemberById/{memberId}")
    public UcenterMemberVo getMemberById(@PathVariable("memberId") String memberId) {

        UcenterMember member = memberService.getById(memberId);

        UcenterMemberVo memberVo = new UcenterMemberVo();

        BeanUtils.copyProperties(member,memberVo);

        //返回
        return memberVo;
    }

    /**
     *  获取会员信息：
     *  方法名：getUserInfoByToken
     *  参数：HttpServletRequest request
     *  返回值：R
     */
    @GetMapping("getUserAllInfo")
    public R getUserAllInfo(HttpServletRequest request) {
        //根据token取用户信息
        String id = JwtUtils.getMemberIdByJwtToken(request);

        UcenterMember member = memberService.getById(id);

        //返回
        return R.ok().data("member",member);
    }

    //根据用户id获取用户信息
    @PostMapping("getUserInfoOrder/{id}")
    public UcenterMemberOrder getUserInfoOrder(@PathVariable String id) {
        UcenterMember member = memberService.getById(id);
        //把member对象里面的值赋值给UcenterMemberOrder对象
        UcenterMemberOrder ucenterMemberOrder = new UcenterMemberOrder();
        BeanUtils.copyProperties(member,ucenterMemberOrder);
        return ucenterMemberOrder;
    }

    //统计某一天的注册人数
    @GetMapping("countRegister/{day}")
    public R countRegister(@PathVariable String day) {
        Integer count = memberService.countRegister(day);
        return R.ok().data("count",count);
    }
}

